<?
require 'global.php';
include(ROOT."common/mail_send.php");
$email_template=new DB(Temail_template,$conn);
// pre($email_template);
if($customer_id>0) { header('Location:member.html'); exit; }

$gee=$customer;
$url='register';
// 错误集 error array
$errors=array();

$action='you guess';
if(isset($_POST["action"]) && $_POST["action"]!="")$action=clean($_POST["action"]);
if($action=='register'){
	$email='';
	$password='';
	if(isset($_POST["email"]) && $_POST["email"]!="") $email=clean($_POST["email"]);
	if(isset($_POST["password"]) && $_POST["password"]!="") $password=clean($_POST["password"]);
	if($email==''){ wheel('./'); exit; }

	$key='you guess'; // safety key 安全码, 过滤非法提交
	if(isset($_POST["key"]) && $_POST["key"]!="") $key=clean($_POST["key"]);
	if($key != md5('ecomgear'.date('Ymd'))){ exit; }

	if(! $gee->check_unique('email', $email)){
		$tem=explode('@',$email);
		$temp=$tem[0];
		$nickname=substr($temp,0,2).'**'.substr($temp,-2);
		$arr=array(
			'email'=>$email,
			'password'=>sha1($password),
			'nickname'=>$nickname,
			'last_login_time'=>date('Y-m-d H:i:s'),
		);
			// 邮件相关
			$var_email=array(
				'%email%'=>$email,
				'%nickname%'=>$nickname,
				'%date%'=>date('Y-m-d'),
				'%datetime%'=>date('Y-m-d H:i:s'),
				'%url%'=>$baseurl,
				'%company%'=>$config['SITE_COMPANY_'.LANG],
			);
			// replace vars 替换变量
			$arr_email = $email_template->get_row(5); // id=5 模板/注册成功
			$subject=$arr_email['subject_'.$lang];
			$body=$arr_email['content_'.$lang];
			foreach ($var_email as $key => $value) {
				$subject=str_replace($key, $value, $subject);
				$body=str_replace($key, $value, $body);
			}

			$body='<!doctype html><html>
			<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
			<title>'.$subject.'</title></head>
			<body>'.$body.'</body></html>';
			$arrmail=array(
				'to'=>array($email),
				'subject'=>$subject,
				'body'=>$body,
				'host'=>EMAIL_HOST,
				'account'=>EMAIL_ACCOUNT,
				'password'=>EMAIL_PASSWORD,
				'port'=>EMAIL_PORT,
				'name'=>EMAIL_NAME,
				'ssl'=>EMAIL_SSL,
			);
		try{
			$gee->insert($arr);
			//setcookie("uemail", $cemail, time()+3600*24*14); // signed up successfully and store info.
			setcookie("customer_nickname", encrypt($nickname), time()+3600*24*7); 
			setcookie("customer_id", encrypt($gee->get_id('email',$email)), time()+3600*24*7);
			
			$tem_url='member.html';
			if($referer!=''){
				$tem_url=encrypt($referer,'decode').'.php';
				setcookie("referer", '', time()-100);
			}
			$errors[]=array('message'=>WEB_SUCCESS,'type'=>'message','url'=>$tem_url);
			// 发邮件
			if(! mail_send($arrmail)) echo 'failed email';
			// 邮件结束
		}
		catch(Exception $e){
			//yell('Failed, please retry.',$url.'.php');
			$errors[]=array('message'=>WEB_REGISTER_FAILED,'type'=>'alert','url'=>'member.html');
		}
	}
	else $errors[]=array('message'=>WEB_EMAIL_EXIST,'type'=>'alert','url'=>'member.html');
}

// SEO setting SEO设置
$title=WEB_REGISTER.' - '.$config['SITE_TITLE_'.LANG];
$dpn=$kwd=$title;

//test
// echo $action;
//test end

// view 加载视图
require VIEW.$url.'.php';

// end of file
